package com.example.freeengine.filetranslate.fragment.send.listadapter;

import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.example.freeengine.R;
import com.example.freeengine.filetranslate.FileTranslateViewModel;
import com.example.freeengine.filetranslate.persistence.entity.FileTranslateDownloadFile;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.concurrent.ExecutionException;

public class FileTranslateDownloadListAdapter extends ListAdapter<FileTranslateDownloadFile, FileTranslateDownloadListAdapter.MyViewHolder> {

    private static final String TAG = FileTranslateDownloadListAdapter.class.getSimpleName();

    Context context;
    FileTranslateViewModel viewModel;

    static DiffUtil.ItemCallback DIFFCALLBACK = new DiffUtil.ItemCallback<FileTranslateDownloadFile>() {
        @Override
        public boolean areItemsTheSame(@NonNull @NotNull FileTranslateDownloadFile oldItem, @NonNull @NotNull FileTranslateDownloadFile newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull @NotNull FileTranslateDownloadFile oldItem, @NonNull @NotNull FileTranslateDownloadFile newItem) {
            return oldItem.equals(newItem);
        }
    };

    public FileTranslateDownloadListAdapter() {
        super(DIFFCALLBACK);
    }

    public FileTranslateDownloadListAdapter(Context context) {
        super(DIFFCALLBACK);
        this.context = context;
    }
    public FileTranslateDownloadListAdapter(Context context, FileTranslateViewModel viewModel) {
        super(DIFFCALLBACK);
        this.context = context;
        this.viewModel = viewModel;
    }

    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new MyViewHolder(layoutInflater.inflate(R.layout.filetanslate_download_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {

        FileTranslateDownloadFile data = getItem(position);

        holder.tvFileName.setText(data.getName());
        holder.tvFileSize.setText(data.getSize() + " Bytes");

        holder.ivMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popup = new PopupMenu(context, v);
                MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(R.menu.menu_filetranslate_download_list_item, popup.getMenu());
                popup.show();

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.filetranslate_download_list_item_menu_delete:{
                                new AlertDialog.Builder(context)
                                        .setTitle("提示")
                                        .setMessage("是否删除该文件?" + "\n" + data.getName())
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                try {
                                                    viewModel.getFileTranslateRepository().delete(data);
                                                    File file = new File(data.getFilePath());
                                                    file.deleteOnExit();
                                                } catch (ExecutionException | InterruptedException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        })
                                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                            }
                                        })
                                        .show();
                                return true;
                            }
                            default:
                                return false;
                        }
                    }
                });
            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvFileName, tvFileSize;
        ImageView ivMenu;

        public MyViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tvFileName = itemView.findViewById(R.id.filetranslate_download_list_item_filename);
            tvFileSize = itemView.findViewById(R.id.filetranslate_download_list_item_size);
            ivMenu = itemView.findViewById(R.id.filetranslate_download_list_item_menu);
        }
    }

}
